<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
import {decode_config, s94_event} from "@/js/s94_tool.js";
import {alert, error_back, iframe, in_right_admin} from "@/js/tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";
import {S94Model} from "@/js/S94Model.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    let model_name = this.$get('model_name', '');
    let model = S94Model.find(model_name);
    if (!model) error_back(`模型不存在：model_name=${model_name}`);

    if(in_right_admin('ModelV2Action/add')) btns['添加'] = function(){ iframe('model_v2_action/edit?'+app.param()) }
    if(in_right_admin('ModelV2Action/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }

    let forms_config = {};

    let status_config = decode_config(model.status_alias_map.status);
    let pay_status_config = decode_config(model.status_alias_map.pay_status);

    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      action:{name:'action', label:'操作名',width:'120px',align:'left'},
      label:{name:'label', label:'别名', width:'100px'},
      type:{name:'type', label:'操作类型',type:'select_one',config:[],width:'100px'},
      operator:{name:'operator', label:'操作者', width:'100px'},

      tips:{name:'tips', label:'介绍', min_width:'120px'},

      need_status:{name:'need_status', label:'状态需求', type:'select_more', config:status_config, width:'140px'},
      need_pay_status:{name:'need_pay_status', label:'支付状态需求', type:'select_more', config:pay_status_config, width:'140px'},

      change_status:{name:'change_status', label:'修改状态', type:'select_one', config:status_config, width:'100px'},
      change_pay_status:{name:'change_pay_status', label:'修改支付状态', type:'select_one', config:pay_status_config, width:'100px'},

      form_param_ids:{name:'form_param_ids', label:'参数ID集', min_width:'200px'},
      execute_ids:{name:'execute_ids', label:'执行ID集', width:'80px'},

      need_log:{label:'记录操作', name:'need_log', type:'radio_switch', value:0, width:'60px'},
      is_ban:{label:'是否禁用', name:'is_ban', type:'radio_switch', config:'禁用|1,启用|0', value:0, width:'60px'},
    };
    list_cols['_action_'] = {name:'action', label:'操作', width:'60px', type:'action', fixed:'right'};

    return {
      model_name: model_name,
      config: {
        api: '/admin/model_v2_action/list?model_name='+model_name,
        title: `${model.label}操作列表${is_del?'(已删除)':''}`,
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
      },
    }
  },
  methods: {
    param(data){
      data = data || {};
      data.model_name = this.model_name;
      return s94Mjs.param_encode(data);
    },
    to_edit(row){
      let can_edit = in_right_admin('ModelV2Action/edit');
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('model_v2_action/edit?'+this.param(param))
    },
    edit(row,col,v){
      let app = this;
      if(!in_right_admin('ModelV2Action/edit')) return;
      ajax(`/admin/model_v2_action/edit?id=${row.id}&${col.name}=${v}`, function (res){
        if (res.data==0) app.$refs['list_url'].list();
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (!this.model_name) {
      if (this.__iframe){
        iframe.remove('model_v2_action/index');
      }else {
        this.$router.back();
      }
      return;
    }

    ajax({url:'/admin/model_v2_action/config_map', cache:true},(res)=>{
      this.config.list_cols.type.config = res.data.type_map;
      return false
    })
  }
}
</script>
